package com.xbongbong.pro.statistic.help.strategy.field.method;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.service.toolbox.statistic.util.ChartResultUtil;
import com.xbongbong.pro.statistic.enums.NumFormatEnum;
import com.xbongbong.pro.statistic.help.FieldAnalysisHelp;
import com.xbongbong.pro.statistic.help.strategy.field.FieldAnalysisStrategy;
import com.xbongbong.pro.statistic.pojo.FieldAnalysisPojo;
import com.xbongbong.pro.statistic.result.pojo.FieldAttrPojo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhouwq
 * @date 2020/9/8 10:27
 */

@Component
public class NumFieldAnalysis implements FieldAnalysisStrategy {

    @Resource
    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;

    @Override
    public Map<String, String> doAnalysisField(FieldAnalysisPojo fieldAnalysisPojo) {
        Map<String, String> results = new HashMap<>(1 << 4);
        FieldAttrPojo field = fieldAnalysisPojo.getField();
        Integer accuracy = field.getAccuracy() == null ? 0 : field.getAccuracy();
        Integer percentUsed = field.getPercentUsed();
        Integer groupingUsed = field.getGroupingUsed();
        Integer numFormat = NumFormatEnum.DEFAULT.getCode();
        List<String> valueResults = fieldAnalysisPojo.getValues();
        List<FieldAttrPojo> summaryFieldList = JsonHelperUtil.parseArray(JSON.toJSONString(fieldAnalysisPojo.getChartEntity().getSummaryAttr()), FieldAttrPojo.class);
        boolean subFormFlag = FieldTypeEnum.isBelongSubForm(field.getParentAttr());
        // 外层循环是tableHead，这里只能用汇总字段attr判一下，再取出
        // 非子表单内字段才会进行设置的置换
        if(!subFormFlag && CollectionUtils.isNotEmpty(summaryFieldList)){
            for (FieldAttrPojo fieldAttrPojo : summaryFieldList) {
                if (Objects.equals(fieldAttrPojo.getBusinessType(), XbbRefTypeEnum.CONTRACT_PERFORMANCE.getCode())) {
                    continue;
                }
                if (Objects.equals(field.getAttr(), fieldAttrPojo.getAttr())) {
                    accuracy = fieldAttrPojo.getAccuracy() == null ? accuracy : fieldAttrPojo.getAccuracy();
                    percentUsed = fieldAttrPojo.getPercentUsed();
                    groupingUsed = fieldAttrPojo.getGroupingUsed();
                    numFormat = fieldAttrPojo.getNumFormat();
                    break;
                }
            }
        }
        // 金额字段展示处理（默认千分位展示）
        if (Objects.equals(1, field.getAmountFlag())) {
            groupingUsed = 1;
        }
        // 使用数值格式
        NumFormatEnum numFormatEnum = NumFormatEnum.getByCode(numFormat);
        String unit = numFormatEnum.getNoYuanUnit();
        for (String value : valueResults) {
            if (StringUtil.isEmpty(value) || Objects.equals(value, StringConstant.SHORT_BARS)) {
                continue;
            }
            String newVal = String.valueOf(ChartResultUtil.formatVal4Num(new BigDecimal(value), numFormatEnum,FieldAnalysisHelp.isPercentUsedOfAccuracy(percentUsed,accuracy)));
            String numberResult = attrDecimalPrecisionHelper.getNumberFormat(newVal, accuracy, groupingUsed, percentUsed);
            if (StringUtil.isNotEmpty(unit)) {
                numberResult += unit;
            }
            results.put(value, numberResult);
        }
        return results;
    }
}
